<? /* 
Plugin Name: Video Wizard
Plugin URI: 
Description: Gestione video
Version: 1.0 
Author: Andrea Ialenti
Author URI: http://www.spaziodaleoni.it
*/
/*  Copyright 2011 Andrea Ialenti  (email : andrea.ialenti@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License 
along with this program; if not, write to the Free Software 
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


require_once 'autoload.php';
require_once 'config.php';
spl_autoload_register("vidwiz_autoload");

register_activation_hook(__FILE__,'vidwiz_install');
register_activation_hook(__FILE__,"vidwiz_tryconvert");
register_deactivation_hook(__FILE__,"vidwiz_uninstall");
//sessioni
add_action('init','vidwiz_session');

//Eventi
add_action('init','vidwiz_test');
add_action('admin_menu', 'vidwiz_vid_menu');
add_action('admin_init','vidwiz_operation');
add_action('init',"vidwiz_convert");
add_action('widgets_init','vidwiz_register_widgets');
add_action('init','vidwiz_check_contests');

//Ajax
add_action('wp_ajax_searchVideosPalinsesto',"vidwiz_searchVideosPalinsesto");
add_action('wp_ajax_nextVideoPalinsesto',"vidwiz_nextVideoPalinsesto");
add_action('wp_ajax_paginationPalinsesto',"vidwiz_paginationPalinsesto");
add_action('wp_ajax_nopriv_nextVideoPalinsesto',"vidwiz_nextVideoPalinsesto");
add_action('wp_ajax_checkPagination',"vidwiz_checkPagination");
add_action('wp_ajax_getContestInfo',"vidwiz_getContestInfo");
add_action("wp_ajax_contestSubscription","vidwiz_contestSubscription");
add_action("wp_ajax_allow_disallowUserInContest","vidwiz_allow_disallowUserInContest");
add_action("wp_ajax_getUploadPage","vidwiz_vid_upload_page_contests");
add_action("wp_ajax_allow_disallowVideoContest","vidwiz_allow_disallowVideoContest");
add_action("wp_ajax_reloadContestsWidget","vidwiz_reloadContestsWidget");
add_action("wp_ajax_showVideoForVoting","vidwiz_showVideoForVoting");
add_action("wp_ajax_deleteUserFromCharts","vidwiz_deleteUserFromCharts");
add_action("wp_ajax_submitScore","vidwiz_submitScore");
add_action("wp_ajax_getContestsToUpload","vidwiz_getContestsToUpload");
add_action("wp_ajax_getActiveContests","vidwiz_getActiveContests");

//Shortcodes
add_shortcode('vidwiz-palinsesto','vidwiz_palinsesto_frontend');
add_shortcode('vidwiz-video','vidwiz_video');
add_shortcode('vidwiz-home','vidwiz_home');

function vidwiz_test(){
    global $config;

}

/**
 * Inizializza la sessione
 */
function vidwiz_session(){
    if (!session_id())
        session_start();
}

/**
 * Installa il plugin
 */
function vidwiz_install(){
    $controller = USingleton_vidwiz::getInstances("CInstallation_vidwiz");
    $controller->installVideos();  
    $controller->installPalinsesto();
    $controller->installContests();
    $controller->installScores();
    $controller->exampleData();
    $controller->createUPVCP();
    $controller->createPalinsesto();
}

/**
 * Disinstalla il plugin
 */
function vidwiz_uninstall(){
    $controller = USingleton_vidwiz::getInstances("CInstallation_vidwiz");
    $controller->uninstall();
}

/**
 * Imposta i menu lato amministratore
 */
function vidwiz_vid_menu() {
    //Inizializza il menu per l'upload
    if(current_user_can('administrator')){
        add_menu_page('Video Wizard', "Video Wizard", "administrator", vidwiz_upload, 'vidwiz_vid_upload_page');
        add_submenu_page(vidwiz_upload,'Video Wizard Breaking News', "Breaking News", "administrator", vidwiz_breaking_news, 'vidwiz_breaking_news_page');
        add_submenu_page(vidwiz_upload,'Video Wizard Contests', "Contests", "administrator", vidwiz_contests, 'vidwiz_contests_page');
        }
    if(current_user_can('author')){
        add_menu_page('Video Wizard', "Video Wizard", "author", vidwiz_upload, 'vidwiz_vid_upload_page');
        add_submenu_page(vidwiz_upload,'Video Wizard Breaking News', "Breaking News", "author", vidwiz_breaking_news, 'vidwiz_breaking_news_page');
        add_submenu_page(vidwiz_upload,'Video Wizard Contests', "Contests", "author", vidwiz_contests, 'vidwiz_contests_page');
        }
    add_submenu_page(vidwiz_upload,'Video Wizard Palinsesto', "Palinsesto", 'administrator', vidwiz_palinsesto, 'vidwiz_vid_palinsesto_or_home_page');
}

/**
 * Visualizza il menu per l'upload per le immagini
 */
function vidwiz_vid_upload_page_contests(){
    $CVideos = USingleton_vidwiz::getInstances("CVideos_vidwiz");
    echo $CVideos->vidwiz_vid_upload_page_contests();
    die();
}

/**
 * Visualizza il menu per l'upload per le immagini
 */
function vidwiz_vid_upload_page(){
    $CVideos = USingleton_vidwiz::getInstances("CVideos_vidwiz");
    echo $CVideos->vidwiz_vid_upload_page();
    die();
}

function vidwiz_breaking_news_page(){
    $CVideos = USingleton_vidwiz::getInstances("CVideos_vidwiz");
    echo $CVideos->vidwiz_breaking_news_page();
}

/**
 * Visualizza il menu per la creazione del palinsesto
 */
function vidwiz_vid_palinsesto_or_home_page(){
    $CPal = USingleton_vidwiz::getInstances("CPalinsesto_vidwiz");
    echo $CPal->vidwiz_vid_palinsesto_or_home_page();
}

/**
 * Recupera l'operazione da effettuare e la esegue
 */
function vidwiz_operation(){
    $view = USingleton_vidwiz::getInstances("View_vidwiz");
    $controller = USingleton_vidwiz::getInstances("Controller_vidwiz");
    $operation = $view->getOperation();
    if($operation != false)
        $controller->executeOperation($operation);
    else
        return;
}

/**
 * Aggiunge gli eventi temporizzati per la conversione (fase d'installazione)
 */
function vidwiz_tryconvert(){
    for($i=0;$i<60;$i=$i+5)
        wp_schedule_event(time()+60*$i,'hourly','goConvert');
}

/**
 * Effettua (tenta) la conversione dei file video non convertiti
 */
function vidwiz_convert(){
    $controller = USingleton_vidwiz::getInstances("CVideos_vidwiz");
    $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
    $controller->convertVideos();

}

/**
 * Cerca i video ed effettua il display dei risultati
 */
function vidwiz_searchVideosPalinsesto(){
    $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
    $controller = USingleton_vidwiz::getInstances("CPalinsesto_vidwiz");
    echo $controller->vidwiz_searchVideosPalinsesto();
}

function vidwiz_reloadContestsWidget(){
    $controller = USingleton_vidwiz::getInstances("CWidgets_vidwiz");
    echo $controller->get_vidwiz_contests_wid();
    die();
}

function vidwiz_showVideoForVoting(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    echo $controller->vidwiz_getVideoInfosContestVoting();
    die();
}


function vidwiz_deleteUserFromCharts(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    echo $controller->vidwiz_deleteUserFromCharts();
    die();
}

function vidwiz_submitScore(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
    
    echo $controller->vidwiz_submitScore();
    die();
}


function vidwiz_getContestsToUpload(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
    
    echo $controller->vidwiz_getContestsToUpload();
    die();
}

function vidwiz_getActiveContests(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
    
    echo $controller->vidwiz_getActiveContests();
    die();
}

/**
 * Ritorna il video da visualizzare nel palinsesto attuale
 */
function vidwiz_nextVideoPalinsesto(){
    $CPal = USingleton_vidwiz::getInstances("CPalinsesto_vidwiz");
    echo $CPal->vidwiz_nextVideoPalinsesto();
}
    
/**
 * Ritorna l'attuale palinsesto
 * @param bool $metadata
 * @return string il palinsesto attuale 
 */
function vidwiz_palinsesto_frontend(){
    $controller = USingleton_vidwiz::getInstances("CPalinsesto_vidwiz");
    return $controller->getActualPalinsestoFrontend();
}

function vidwiz_check_contests(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    $controller->checkContests();
}

function vidwiz_video($vars = false){
    $controller = USingleton_vidwiz::getInstances("CVideos_vidwiz");
    $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
    
    if($vars != false)
        return $controller->getVideoFrontend($vars['id']);
    else return false;
}

/**
 * Funzione utile per la gestione della paginazione. Viene richiamata quando un utente
 * richiede di visualizzare la successiva (precedente) pagina di risultati
 */
function vidwiz_paginationPalinsesto(){
    $controller = USingleton_vidwiz::getInstances("CPalinsesto_vidwiz");
    echo $controller->vidwiz_paginationPalinsesto();
}

function vidwiz_home(){
    $controller = USingleton_vidwiz::getInstances("CPalinsesto_vidwiz");
    echo $controller->vidwiz_vid_palinsesto_or_home_page(true);
}

function vidwiz_contests_page(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    echo $controller->vidwiz_contests_page();
}

function vidwiz_getContestInfo(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    echo $controller->vidwiz_contests_info();
    die();
}

function vidwiz_contestSubscription(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    echo $controller->vidwiz_contests_subscription();
    die();
}

function vidwiz_allow_disallowUserInContest(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
    echo $controller->vidwiz_allow_disallowUserInContest();
    die();
}

function vidwiz_allow_disallowVideoContest(){
    $controller = USingleton_vidwiz::getInstances("CContests_vidwiz");
    echo $controller->vidwiz_allow_disallowVideoInContest();
    die();
}

/**
 * Questa funzione viene eseguita a seguito della richiesta di una pagina successiva
 * nei risultati della ricerca di video (durante, ad esempio, la creazione di un palinsesto).
 * In particolare serve per vedere se esistono pagine di risultati successive o precedenti
 * alla ultima pagina di risultati inviata.
 */
function vidwiz_checkPagination(){
    $debug = USingleton_vidwiz::getInstances("UDebug_vidwiz");
    $controller = USingleton_vidwiz::getInstances("CPalinsesto_vidwiz");
    echo $controller->vidwiz_checkPagination();
}

//Registra i widget
function vidwiz_register_widgets(){
    register_widget('vidwiz_searchbox_wid');
    register_widget('vidwiz_contests_wid');
}


class vidwiz_searchbox_wid extends WP_Widget {
    /**
     * Costruttore che impost i campi di configurazione del widget
     */
    public function vidwiz_searchbox_wid(){
        $widget_ops = array(
            'classname' => 'vidwiz_searchbox_wid',
            'descritpion' => __("Widget per la ricerca dei video nel lato front-end del blog","video_wizard")
          
        );
        $this->WP_Widget("vidwiz_searchbox_wid",__("Searchbox Video Wizard","video_wizard"), $widget_ops);
        }

   public function widget($args,$instance){
       extract($args);
       $controller = USingleton_vidwiz::getInstances("CWidgets_vidwiz");
       echo $before_widget;
       $title = "Ultimi video";
       echo $before_title.$title.$after_title;
       $wid =  $controller->get_vidwiz_searchbox();
       echo $wid;
       echo $after_widget;
   }
}
   
class vidwiz_contests_wid extends WP_Widget {
    /**
     * Costruttore che imposta i campi di configurazione del widget
     */
    public function vidwiz_contests_wid(){
        $widget_ops = array(
            'classname' => 'vidwiz_contests_wid',
            'descritpion' => __("Widget per la visualizzazione dei contests","video_wizard")
          
        );
        $this->WP_Widget("vidwiz_contests_wid",__("Contests Video Wizard","video_wizard"), $widget_ops);
        }

   public function widget($args,$instance){
       extract($args);
       $controller = USingleton_vidwiz::getInstances("CWidgets_vidwiz");
       echo $before_widget;
       $title = "Contests attivi";
       echo $before_title.$title.$after_title;
       $wid =  $controller->get_vidwiz_contests_wid();
       echo $wid;
       echo $after_widget;
   }
   
}
?>